/*
 * @Author: wurangkun wurangkun@qq.com
 * @Date: 2023-05-26 11:05:55
 * @LastEditors: wurangkun wurangkun@qq.com
 * @LastEditTime: 2023-05-31 17:49:13
 * @FilePath: \myblog_server\utils\file\upload.ts
 * @Description: 文件上传
 */
import { UPLOADURL } from "src/config"
import file from "src/controller/file"
import { UploadOption } from "src/typings/file"
const fs = require('fs')
const path = require('path')

const uploadDir = path.join(__dirname, `./${UPLOADURL}`)

/**
 * 保存文件
 */
export const saveFile = (fileOption: UploadOption,ck:Function) => {
  const { files } = fileOption
  // @ts-ignore
  const { filepath,newFilename } = files
  // const fileDir = path.join(uploadDir, originalFilename)
  // 创建可读流
  const reader = fs.createReadStream(filepath);
  let filePath = path.join(__dirname, '../../uploads/') + `${newFilename}`;
  // 创建可写流
  const upStream = fs.createWriteStream(filePath);

  // 可读流通过管道写入可写流
  reader.pipe(upStream);
  // 回调将数据存入数据库
  ck&&ck({
    path:filePath
  })
  // fs.writeFileSync(fileDir, upStream)
}
//   fs.writeFileSync(uploadDir, file.files)
//   console.log(file);
// }
